package com.devfest.wishmaster.gui;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.devfest.wishmaster.AddWishlistItemActivity;
import com.devfest.wishmaster.R;
import com.devfest.wishmaster.model.WishList;

public class DetailActivity extends Activity {
	
	WishList wishList;
	ListView detailListView;
	DetailAdapter adapter;
	int position;
	
	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wish_list_details);
		
		Intent intent = getIntent();
		position = intent.getIntExtra("position", 0);
		wishList = MyApplication.getInstance().wishLists.get(position);
		setTitle(wishList.getName());
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		TextView name = (TextView) findViewById(R.id.detail_wishlist_name);
		name.setText(wishList.getName() + "(" + formatter.format(wishList.getEventDate()) + ")");
		TextView created = (TextView) findViewById(R.id.detail_date_created);
		created.setText("Date Created:" + formatter.format(wishList.getDateCreated()));
		
		detailListView = (ListView) findViewById(R.id.detail_listview);
		adapter = new DetailAdapter(this, wishList.getWishItems());
		detailListView.setAdapter(adapter);
		detailListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(DetailActivity.this, CommentsActivity.class);
				intent.putExtra("wish_list_position", position);
				intent.putExtra("wish_item_position", arg2);
				startActivity(intent);
			}
		});
	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.wish_item_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.add_wish_item:
				Intent intent = new Intent(DetailActivity.this, AddWishlistItemActivity.class);
				intent.putExtra("wish_list_position", position);
				
				startActivity(intent);
				break;
			default:
				break;
			}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		adapter.notifyDataSetChanged();
		MyApplication.getInstance().setActivity(this);
	}
}
